<?php
$action_ary = array(
	'importxls'=>'device_act_importxls'
);


//导入数据
function device_act_importxls(){
	$obj=new import();
	
	$_file=$obj->save_file('xls');
    if(!$_file)exit('上传失败');
    
    $pid=request('pid'); //项目id
    $type=request('type'); //类型
    
    $rtn_type=array(1=>'挖机',2=>'线路',3=>'机手',4=>'地块',5=>'司机',6=>'油罐',7=>'设备',8=>'员工',9=>'施工员');   			
	$res=$obj->importExecl($_file);
	if(empty($res))exit('数据为空');
	
	switch ($type){
		case 1: 
		case 4:
		case 6: $sql=sql_wjsz($res,$pid,$type);break;
        case 2:
		case 10: $sql=sql_wjszyunj($res,$pid,$type);break;
		case 3:
		case 5:
		case 9:
		case 8: $sql=sql_sjsz($res,$pid,$type);break;
		case 7: $sql=sql_jysb($res,$pid,$type);break;
	}

	if(empty($sql))return;

	$device_obj=new device();
	//清空原数据
	clear_before($device_obj,$pid,$type);
	
	$device_obj->device_insert_bat($sql);	
	
	unlink($_file);
	exit('导入成功');
}

/**
 * 清空原始数据
 * @param int $pid   项目id
 * @param int $type  数据类型
 */
function clear_before($obj,$pid,$type){	
	$sql="DELETE FROM syt_set WHERE uid={$pid} AND type={$type}";
	$obj->_mysql(0)->query($sql);
}


//挖机设置数据库语句
function sql_wjsz($res,$pid,$type){
	$len=count($res);
	foreach($res as $key=>$vo){
		if($key==1)continue;	
		$sql .=",({$pid},{$type},'{$vo['A']}','{$vo['B']}',{$len})";
		$len--;
	}
	
	$sql="INSERT INTO syt_set (`uid`,`type`,`code`,`comment`,`sort`) VALUES ".trim($sql,',');
	return $sql;	
}


//挖机设置数据库语句
function sql_wjszyunj($res,$pid,$type){
    $len=count($res);
    foreach($res as $key=>$vo){
        if($key==1)continue;
        $sql .=",({$pid},{$type},'{$vo['A']}','{$vo['B']}','{$vo['C']}',{$len})";
        $len--;
    }

    $sql="INSERT INTO syt_set (`uid`,`type`,`code`,`yun_min`,`yun_max`,`sort`) VALUES ".trim($sql,',');
    return $sql;
}


//司机设置数据库语句
function sql_sjsz($res,$pid,$type){
	$len=count($res);
	foreach($res as $key=>$vo){
		if($key==1)continue;
		$sql .=",({$pid},{$type},'{$vo['A']}','{$vo['B']}','{$vo['C']}',{$len})";
		$len--;
	}

	$sql="INSERT INTO syt_set (`uid`,`type`,`code`,`comment`,`phone`,`sort`) VALUES ".trim($sql,',');
	return $sql;
}

//司机设置数据库语句
function sql_jysb($res,$pid,$type){
	$len=count($res);
	foreach($res as $key=>$vo){
		if($key==1 || empty($vo['A']))continue;
		$sql .=",({$pid},{$type},'{$vo['A']}','{$vo['B']}','{$vo['C']}','{$vo['D']}',{$len})";
		$len--;
	}

	$sql="INSERT INTO syt_set (`uid`,`type`,`code`,`cartype`,`comment`,`phone`,`sort`) VALUES ".trim($sql,',');
	return $sql;
}